package com.jyw.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.jyw.common.core.domain.entity.SysDictData;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Created by ZhangCheng on 2024/01/08
 */
@UtilityClass
public class DictNameUtil {

    /**
     * 多个字典值转名称
     *
     * @param values
     * @param dictList
     * @return
     */
    public String getNameByValues(String values, List<SysDictData> dictList) {
        if (StrUtil.isNotEmpty(values) && CollectionUtil.isNotEmpty(dictList)) {
            List<String> labels = Arrays.asList(values.split(","));
            List<String> nameList = new ArrayList<>();
            labels.forEach(a -> {
                SysDictData devDict = dictList.stream().filter(d -> d.getDictValue().equals(a)).findFirst().orElse(null);
                if (devDict != null && StrUtil.isNotEmpty(devDict.getDictLabel())) {
                    nameList.add(devDict.getDictLabel());
                }
            });
            String names = nameList.stream().collect(Collectors.joining(","));
            return names;
        }
        return null;
    }
}
